WARNING: This program has a logic error. The cow is hungry and wants to eat the hay, but nothing happens!

RULE: An event handler links a sprite to a function that is called whenever the event is triggered.

  • Click Run and see what happens. Look in the code to see where the cow should be eating the hay.
  • Fix the program by adding an event handler to the main() function so that the collision event happens.
  • Click Run to test if you fixed the program. When it is fixed, click Submit and Next.

To navigate the page using the TAB key, first press ESC to exit the code editor.

def add_sprite(): """ Add cow sprite to the stage and set its speed """ cow = codesters.Sprite("cow",-400, -150) cow.set_x_speed(3) return cow def setup_stage(): """ Set up the stage with background and sprites """ stage.set_background("barn") stage.disable_right_wall() hay = codesters.Sprite("hay",-150,-150) hay = codesters.Sprite("hay",0,-150) hay = codesters.Sprite("hay",150,-150) def collision(sprite, hit_sprite): """ Collision event for the cow to eat hay """ if hit_sprite.get_image_name() == "hay": sprite.say("yum!",.2) hit_sprite.hide() def main(): """ Sets up the program and calls other functions """ setup_stage() sprite = add_sprite() main()
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)